﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using TTSBLL;
using TTS;
using TTSModel;

public partial class _Default : System.Web.UI.Page
{
    private Assemble a;
    private AssembleItem ai;
    //private AssembleInfoQuerying assembleInfoQueryingObj;
   // private AssembleItemInfoQuerying assembleItemInfoQueryingObj;
    private AssembleInfo ainfo;
    private IList <AssembleItemInfo> aItemInfoList;
    private KCCSNUnion kccQuerying;
    IList<AssembleInfo> ainfos;
    private IList <AssembleInfoQuerying> assembleInfoList;
    private IList <AssembleItemInfoQuerying> assembleItemInfoList;
    private string sn;
    private string message;

    public _Default()
    {
        a = new Assemble();
        ai = new AssembleItem();
       // assembleInfoQueryingObj = new AssembleInfoQuerying();
        kccQuerying = new KCCSNUnion();
        
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if(Request.QueryString["SN"] != null)
            {
                sn = Request.QueryString["SN"].ToString();
                sn = CommonHelper.GetSN_EitherSNorKCCSN(sn);
                BindData();
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        sn = this.TextBox1.Text.Trim();
        sn = CommonHelper.GetSN_EitherSNorKCCSN(sn);
        BindData();
    }

    protected void BindData()
    {
        try
        {
            ainfo = a.Assemble_GetbySN(sn);
            ainfos = new List<AssembleInfo>();
            ainfos.Add(ainfo);
            assembleInfoList = new List<AssembleInfoQuerying>();
            for (int i = 0; i < ainfos.Count; i++)
            {

                assembleInfoList.Add(new AssembleInfoQuerying(ainfos[i].ID, ainfos[i].SN, kccQuerying.KCCSNUnion_GetbySN(ainfos[i].SN).KCCSN1, ainfos[i].Tcode, ainfos[i].RecordTime, ainfos[i].Userid, ainfos[i].IsRoot));
            }

            // this.GridView1.DataSource = ainfos;
            this.GridView1.DataSource = assembleInfoList;
            this.GridView1.DataBind();
            // this.GridView2.DataSource = ai.AssembleItem_GetbyID(ainfo.ID);
            //this.GridView2.DataBind();
            aItemInfoList = ai.AssembleItem_GetbyID(ainfo.ID);
            assembleItemInfoList = new List<AssembleItemInfoQuerying>();
            for (int i = 0; i < aItemInfoList.Count; i++)
            {
                assembleItemInfoList.Add(new AssembleItemInfoQuerying(aItemInfoList[i].ID, aItemInfoList[i].SN, aItemInfoList[i].SubSN, kccQuerying.KCCSNUnion_GetbySN(aItemInfoList[i].SubSN).KCCSN1, aItemInfoList[i].Tcode, aItemInfoList[i].Remark, aItemInfoList[i].IsLeaf));
            }
            this.GridView2.DataSource = assembleItemInfoList;
            this.GridView2.DataBind();
        }
        catch
        {
            message = "数据库中可能已经没有某些装配件的标签信息！";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
        }
       
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string this_sn = GridView1.SelectedDataKey["SN"].ToString();
        string output_sn = ai.LoadEntity(this_sn).SN;
        Response.Redirect("~/search/Default.aspx?SN=" + output_sn);
    }

    protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (bool.Parse(GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString()))
            {
                e.Row.Cells[0].Text = "";
            }
        }
        //遍历所有行设置边框样式
        foreach (TableCell tc in e.Row.Cells)
        {
            tc.Attributes["style"] = "border-color:Black";
        }
    }

    protected void GridView2_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (bool.Parse(GridView2.DataKeys[e.Row.RowIndex].Values[0].ToString()))
            {
                e.Row.Cells[5].Text = "";
            }
        }
        //遍历所有行设置边框样式
        foreach (TableCell tc in e.Row.Cells)
        {
            tc.Attributes["style"] = "border-color:Black";
        }
    }
    protected void Return_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/search/searchMainHtml.aspx");
    }
}
